import type { PageType } from '../../../../../lib/page_handler/type'
import type {
  BaseStyleConfig,
  IPersonalizationItemConfig,
  PlatformTypeValue,
} from '../../../../../types/interfaces'
import { TextInput } from '../../../../../components/text_input'
import { UserPref } from '../../../../../lib/abstract'
import { BaiduPageType } from '../../../../../lib/page_handler/platform/baidu/types'
import {
  SUPPORTED_PLATFORMS,
  VALID_RESULT,
} from '../../../../../types/constants'

export class PopularLink extends UserPref {
  readonly id = 'popular_link'
  readonly name = 'PopularLink'
  readonly styleConfigKey = 'popularLink'
  readonly supportedPages: Partial<Record<PlatformTypeValue, PageType[]>> | '*' = {
    [SUPPORTED_PLATFORMS.BAI_DU]: [
      BaiduPageType.Web,
    ],
  }

  readonly cssProperty = ''
  readonly cssValueSuffix = ''
  // 由于文本输入使用的是失焦事件和回车事件，不会短时间高频触发事件，所以不需要防抖保存处理，故置为0
  readonly debouncedSaveTimeout: number = 0

  constructor(config: IPersonalizationItemConfig) {
    super(config)
  }

  createComponent(config: BaseStyleConfig, value: string, _mode: string, debouncedSave: (() => void) & { flush: () => void }): TextInput {
    const id = this.generateId(config.styleVar)
    return new TextInput({
      id,
      label: '热门网站',
      type: 'textarea',
      rows: 8,
      placeholder: `输入需要高亮的网址（每行一个），支持完整链接或开头部分匹配，刷新后生效`,
      value,
      onChange: (value) => {
        this.cachedValues.set(id, String(value))
        debouncedSave()
      },
    })
  }

  // 填写内容复杂，暂不需要额外的校验逻辑
  handleCheck(_value: string): { status: ValidResultType, error: string } {
    // 支持输入为空或长度为0
    return { status: VALID_RESULT.VALID.value, error: '' }
  }

  applyStyles(): void {}
}
